<template>
  <span>{{ labelText }}</span>
</template>

<script>
  import { getAction } from '@/api/manage'
  import { getDictItemsFromCache } from '@/api/api'

  export default {
    name: 'DictLabel',
    props: {
      value: {
        type: [String, Number, Boolean],
        required: false
      },
      dictCode: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        dictOptions: [],
        labelText: ''
      }
    },
    watch: {
      value: {
        immediate: true,
        handler() {
          this.initDictData();
        }
      },
      dictCode: {
        immediate: true,
        handler() {
          this.initDictData();
        }
      }
    },
    methods: {
      initDictData() {
        // 如果value或dictCode为空，则不处理
        if (!this.value || !this.dictCode) {
          this.labelText = '';
          return;
        }
        
        // 从缓存中获取字典数据
        getDictItemsFromCache(this.dictCode).then(res => {
          if (res && res.length > 0) {
            this.dictOptions = res;
            // 查找匹配的字典项
            const dictItem = this.dictOptions.find(item => item.value === this.value);
            this.labelText = dictItem ? dictItem.text : this.value;
          } else {
            this.labelText = this.value;
          }
        });
      }
    }
  }
</script> 